// miniprogram/pages/q3/q3.js
var utils = require('../../utils/getTool');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lineCode:"",
    showAdd:true,
    runningStatus:"",
    radio: '1',
    activeNames: ['0'],
    active: 0,
    runningStatusList:[
      {
        name:"--请选择--",
        value:0
      }, {
        name: "正常",
        value: 1
      },
      {
        name: "检修中",
        value: 2
      }],
    showisStartList:[{name:"启用",value:1},{name:"停用",value:0}],
    upLineInfo:{
      common: null,
      endPole: { id:0,poleCode: null },
      isStart: 0,
      lineCode: null,
      lineName:null,
      lineLength: null,
      runningStatus: null,
      runningStatusName: null,
      startPole: { id:0,poleCode:null},
      towerBaseNum: null,
      voltageLevel: null,
      backLength: null
    }
  },
  onLineCode(e){
    this.data.upLineInfo.lineCode = e.detail;
  },
  onLineName(e) {
    this.data.upLineInfo.lineName = e.detail;
  },
  saveLineInfo() { //新增
    if (this.data.upLineInfo.endPole.poleCode === this.data.upLineInfo.startPole.poleCode) {
      wx.showToast({
        title: '起始杆号与终止杆号不能一致！',
        icon: "none"
      })
      return;
    }
    wx.showLoading({
      title: '请稍等...',
    })
    var dat = this.data.upLineInfo;
    var obj = {
        id: dat.id,
        common: dat.common,
        endPole: { id: dat.endPole.id },
        isStart: dat.isStart,
        lineCode: dat.lineCode,
        lineName: dat.lineName,
        lineLength: dat.lineLength,
        runningStatus: dat.runningStatus,
        runningStatusName: dat.runningStatusName,
        startPole: { id: dat.startPole.id },
        towerBaseNum: dat.towerBaseNum,
        voltageLevel: dat.voltageLevel,
        backLength: dat.backLength,
        createdBy: this.data.userInfo.openid
    }; //声明一个对象
    console.log(JSON.stringify(obj))
    utils.req({
      url:getApp().appData.site+"/accp/psLine/upLineInfo",
      data: {
        byjson:JSON.stringify(obj)
      },
      success:res=>{
        wx.hideLoading();
        if(res.data){
          wx.showToast({
            title: '操作成功!',
            icon:"none"
         })
        }
        this.setData({
          active:0
        });
        this.quelineInfo();
      }
    })
  },
  upTapisStart(){
    this.setData({
      showTapisStart:true
    })
  },
  hidTapisStrat() {
    this.setData({
      showTapisStart: false
    })
  },
  seleTapisStart(e){ //切换启用状态
    this.data.upLineInfo.isStart = e.detail.value;
    this.data.upLineInfo.isStratStr = e.detail.name;
    this.setData({
      upLineInfo: this.data.upLineInfo,
      showTapisStart:false
    })
  },
  seleEndPole(e){ //选择停止杆号
    this.data.upLineInfo.endPole.poleCode = e.detail.name;
    this.data.upLineInfo.endPole.id = e.detail.value;
    this.setData({
      upLineInfo: this.data.upLineInfo, //将你所选的数据再次填充到这个集合里面
      showEndPole: false
    })
  },
  tapEndPole(){
    this.setData({
      showEndPole:true
    })
  },
  hidEndPole() {
    this.setData({
      showEndPole: false
    })
  },
  tapUpIsStart(e){ //删除
    utils.req({
      url: getApp().appData.site+"/accp/psLine/upIsStart",
      data: {
        id: e.target.dataset.id,
        isStart: e.target.dataset.isstart
      },
      success: res => {
        this.quelineInfo(); //删除成后查询本页面数据
      }
    })
  },
  tapModify(e) { //点击修改按钮时
    console.log(this.data)
    //poleInfoList: utils.getPoleInfo, //数据库查询 杆塔信息
    this.setData({
      showAdd:false, //隐藏新增
      active: 1,
      upLineInfo: this.data.showLinePoleList[e.target.dataset.index] //你点击了那个数据 根据你点击的索引传递值
    })
  },
  /**
   * 获得杆塔信息以及线路
   */
  getAllLineAndPole(){ //
    utils.req({
      url:getApp().appData.site+"/accp/psLine/getAllLine",
      success: (res) => {
        console.log("得到所有杆塔信息",res)
        var list = [];
        for(var i in res.data){
          list.push({
            name: res.data[i].lineName,
            value: res.data[i]
          })
        }
        this.setData({
          lineNameList: list
        })
      },fail:err=>console.log(e)
    })
    utils.getPoleInfo({
      success: res => {
        this.setData({
          stratInfoLine: res //起始杆号
        })
      }
    });
  },
  seleStratPole(e) {
    this.data.upLineInfo.startPole.poleCode = e.detail.name;
    this.data.upLineInfo.startPole.id = e.detail.value;
    this.setData({
      upLineInfo:this.data.upLineInfo , //将你所选的数据再次填充到这个集合里面
      showPoleInfo: false
    })
  },
  tapStratPole(){
    this.setData({
      showPoleInfo: true,
    })
  },
  hidPoleInfo(){
    this.setData({
      showPoleInfo: false
    })
  },
  delLineInfo(e){
    wx.showModal({
      title: '提示',
      content: '你确定要删除吗?',
      success:res=>{
        if(res.confirm){
          utils.req({
            url:getApp().appData.site+"/accp/psLine/del",
            data:{
              id: e.target.dataset.id
            },
            success:res=>{
              if(res.data){wx.showToast({
                title: '删除成功!',
                icon:"none"
              })}
              this.quelineInfo(); //删除成后查询本页面数据
            }
          })
        }
      }
    })
  },
  quelineInfo(){
    utils.req({
      url:getApp().appData.site+"/accp/psLine/getAllLineList",
      data:{
        lineCode: this.data.lineCode,
        runningStatus: this.data.runningStatus
      },
      success:(res)=>{
        this.setData({
          showLinePoleList: res.data
        })
      }
    })
  },
  hideisStart(){
    this.setData({
      showisStart: false
    })
  },
  seleRunning(e){
    this.setData({
      inpRunning:e.detail.name,
      runningStatus:e.detail.value,
      showisStart:false
    })
  },
  inplineCode(e) { 
    this.data.lineCode = e.detail
  },
  onshowIsStart(){
    this.setData({
      showisStart:true
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAllLineAndPole();
    this.setData({
      userInfo:wx.getStorageSync('userInfo')
    })
    this.quelineInfo();
  },
  xx() {
    this.setData({
      active: 0
    })
  },
  onChange(event) {
    wx.showToast({
      title: `切换到标签 ${event.detail.name}`,
      icon: 'none'
    });
  },
  radioChange(e) {
    console.log(e)
    this.data.upLineInfo.isStart = e.detail
    this.setData({
      upLineInfo: this.data.upLineInfo
    });
  },
  onChanges(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  onlineLength(e){
    this.data.upLineInfo.lineLength = e.detail
  },
  ontowerBaseNum(e) {
    this.data.upLineInfo.towerBaseNum = e.detail
  },
  oncommon(e) {
    this.data.upLineInfo.common = e.detail
  },
  onbackLength(e) {
    this.data.upLineInfo.backLength = e.detail
  },
  onvoltageLevel(e) {
    this.data.upLineInfo.voltageLevel = e.detail
  }
})